package com.ck.ips.controller;

import com.ck.common.model.DeviceStore;
import com.ck.common.model.OperationResult;
import com.ck.common.page.PageInfo;
import com.ck.core.annotation.LoginRequired;
import com.ck.core.annotation.ResourcePermissionRequired;
import com.ck.core.annotation.SysLog;
import com.ck.core.constant.CharsetConstant;
import com.ck.core.constant.ModuleConstant;
import com.ck.ips.model.vo.device.DeviceBasicInfoVO;
import com.ck.ips.service.DeviceService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @Description:  设备相关controller
* @Author: hf
* @Date:    2019/5/7 16:31
* @Version: 1.0
*/
@LoginRequired
@RestController
@RequestMapping("/device")
public class DeviceController extends BaseController {
	@Autowired
	private DeviceService deviceService;

	/**
	 * 分页查询设备信息
	 * @param pageNum
	 * @param pageSize
	 * @param groupId
	 * @param groupName
	 * @param deviceName
	 * @param deviceMac
	 * @param ip
	 * @param deviceStatus
	 * @param bindingStatus
	 * @param deviceSerialNum
	 * @param deviceType
	 * @param resolvingPower
	 * @param screenSize
	 * @param belowSoftVersion
	 * @return
	 */
	@PostMapping(value = "/list")
	public Object listByPage( Integer pageNum, Integer pageSize,String groupId, String groupName, String deviceName, String deviceMac, String ip
			,String deviceStatus, Integer bindingStatus,String deviceSerialNum, String deviceType, String resolvingPower
			,String screenSize,Integer belowSoftVersion,String storeName,Boolean programDue) {

        PageInfo<DeviceBasicInfoVO> deviceList = deviceService.listBasicDeviceInfoByPage(pageNum, pageSize, groupId, deviceName, deviceMac, ip, deviceStatus, deviceSerialNum, deviceType, resolvingPower, bindingStatus, screenSize, belowSoftVersion, groupName, storeName,programDue,null);
        Map<String,Object> data = new HashMap<>();
        data.put("pageNum", deviceList.getPageNum());
        data.put("pageSize", deviceList.getPageSize());
        data.put("list", deviceList.getList());
        data.put("total", deviceList.getTotal());
        Integer totalOnline = deviceList.getTotal() == 0 ? 0 : deviceList.getList().stream().filter(e -> "在线".equals(e.getDeviceStatus())).collect(Collectors.toList()).size();
        data.put("totalOnline",totalOnline);
        data.put("totalOffline", deviceList.getTotal() - totalOnline);
        return data;

	}

	/**
	 * 导出终端excel
	 */
	@GetMapping(value="exportDeviceExcel")
	public void exportDeviceExcel(HttpServletResponse response,Integer pageNum, Integer pageSize,String groupId, String groupName, String deviceName, String deviceMac, String ip
			,String deviceStatus, Integer bindingStatus,String deviceSerialNum, String deviceType, String resolvingPower
			,String screenSize,Integer belowSoftVersion,String storeName,Boolean programDue) throws IOException{
		//装入excel表中
		XSSFWorkbook workbook = deviceService.exportDeviceExcel(pageNum, pageSize, groupId, deviceName, deviceMac, ip, deviceStatus, deviceSerialNum, deviceType, resolvingPower, bindingStatus, screenSize, belowSoftVersion, groupName, storeName,programDue);
		//准备将Excel的输出流通过response输出到页面下载
		//输出流
		response.setContentType("application/ms-excel");
		response.setCharacterEncoding(CharsetConstant.UTF8);
		//这后面可以设置导出Excel的名称，此例中名为student.xls
		response.addHeader("Content-Disposition",
				"attachment;filename=" + URLEncoder.encode("设备列表.xlsx", CharsetConstant.UTF8));
		//刷新缓冲
		response.flushBuffer();
		//workbook将Excel写入到response的输出流中，供页面下载
		workbook.write(response.getOutputStream());
	}

	/**
	 * 导出终端excel
	 */
	@GetMapping(value = "exportDeviceProgramListExcel")
	public void exportDeviceProgramListExcel(HttpServletResponse response, Integer pageNum, Integer pageSize, String groupId, String groupName, String deviceName, String deviceMac, String ip
			, String deviceStatus, Integer bindingStatus, String deviceSerialNum, String deviceType, String resolvingPower
			, String screenSize, Integer belowSoftVersion, String storeName, Boolean programDue, Long startTime, Long endTime) throws IOException {
		//装入excel表中
		XSSFWorkbook workbook = deviceService.exportDeviceProgramListExcel(pageNum, pageSize, groupId, deviceName, deviceMac, ip, deviceStatus, deviceSerialNum, deviceType, resolvingPower, bindingStatus, screenSize, belowSoftVersion, groupName, storeName, programDue,startTime,endTime);
		//准备将Excel的输出流通过response输出到页面下载
		//输出流
		response.setContentType("application/ms-excel");
		response.setCharacterEncoding(CharsetConstant.UTF8);
		//这后面可以设置导出Excel的名称，此例中名为student.xls
		response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("设备列表.xlsx", CharsetConstant.UTF8));
		//刷新缓冲
		response.flushBuffer();
		//workbook将Excel写入到response的输出流中，供页面下载
		workbook.write(response.getOutputStream());
	}


	/**
	 * 终端详情页面
	 * @param deviceCode 终端code
	 * @return
	 */
	@PostMapping(value = "/detail")
	public Object detail( String deviceCode ) {
		return deviceService.getDetailByDeviceCode(deviceCode);
	}

	/**
	 * 更新终端详情部分信息
	 * @param deviceCode 终端code地址
	 * @return
	 */
	@PostMapping(value="/update/{deviceCode}")
	public OperationResult updateDeviceDetail(@PathVariable String deviceCode,String deviceType,String screenSize,String resolvingPower,
											  String deviceName,String building,String unit,String floor,String position, String model) {
		DeviceStore modify = new DeviceStore();
		modify.setDeviceCode(deviceCode);
		modify.setBuilding(building);
		modify.setUnit(unit);
		modify.setFloor(floor);
		modify.setPosition(position);
		modify.setType(deviceType);
		modify.setScreeSize(screenSize);
		modify.setResolvingPower(resolvingPower);
		modify.setDeviceName(deviceName);
		modify.setModel(model);
		return deviceService.updateDeviceDetail(modify);
	}

	/**
	 *  远程关机指令
	 */
	@ResourcePermissionRequired
	@PostMapping(value = "/shutdown")
	@SysLog(module = ModuleConstant.MODULE_DEVICE_MNG)
	public OperationResult shutdown(String deviceCodes, String groupIds) {
		return deviceService.shutdown(deviceCodes);
	}

	/**
	 * 重启
	 */
	@ResourcePermissionRequired
	@PostMapping(value = "/restart")
	@SysLog(module = ModuleConstant.MODULE_DEVICE_MNG)
	public OperationResult restart(String deviceCodes, String groupIds) {
		return deviceService.restart(deviceCodes);
	}

	/**
	 * 设置定时开关机
	 */
	@ResourcePermissionRequired
	@PostMapping(value = "/setTimerShutDownOrStart")
	@SysLog(module = ModuleConstant.MODULE_DEVICE_MNG)
	public OperationResult setTimerShutDownOrStart(String deviceCodes, String groupIds, String bootTime, String offTime, String nonWorkDayBootTime, String nonWorkDayOffTime) {
		return deviceService.setTimerShutDownOrStart(deviceCodes,bootTime,offTime,nonWorkDayBootTime,nonWorkDayOffTime);
	}

	/**
	 * WiFi探针开关
	 */
	@ResourcePermissionRequired
	@PostMapping(value = "/wifiProbeSwitch")
	public OperationResult wifiProbeSwitch(String deviceCodes, String groupIds,Integer status){
		return deviceService.wifiProbeSwitch(deviceCodes,status);
	}
	/**
	 * 人脸识别开关
	 */
	@ResourcePermissionRequired
	@PostMapping(value = "/faceRecognitionSwitch")
	public OperationResult faceRecognitionSwitch(String deviceCodes, String groupIds,Integer status){
		return deviceService.faceRecognitionSwitch(deviceCodes,status);
	}

	/**
	 * 设置定时开关屏
	 * @param deviceCodes
	 * @param groupIds
	 * @param bootTime
	 * @param offTime
	 * @return
	 */
	@ResourcePermissionRequired
	@PostMapping(value = "/setScreenTimerShutDownOrStart")
	@SysLog(module = ModuleConstant.MODULE_DEVICE_MNG)
	public OperationResult setScreenTimerShutDownOrStart(String deviceCodes, String groupIds, String bootTime, String offTime) {
		return deviceService.setScreenTimerShutDownOrStart(deviceCodes,bootTime,offTime);
	}

	/**
	 * 设置音量
	 */
	@ResourcePermissionRequired
	@PostMapping(value = "/setVolume")
	@SysLog(module = ModuleConstant.MODULE_DEVICE_MNG)
	public OperationResult setVolume(String deviceCodes, String groupIds, Integer deviceVolume ) {
		return deviceService.setVolume(deviceCodes,deviceVolume);
	}

	/**
	 * 解绑终端
	 */
	@ResourcePermissionRequired
	@PostMapping(value =  "/untie")
	@SysLog(module = ModuleConstant.MODULE_DEVICE_MNG)
	public Object untie(String deviceCodes) {
		return deviceService.untie(deviceCodes);
	}


}
